<template>
  <card>
    <p slot="title">
        <Icon type="compose"></Icon>
        基本信息
    </p>
    <Form ref="formItem" :model="formItem" :rules="ruleForm" :label-width="120">
        <Row type="flex">
            <Col span="6">
                <FormItem label="设备名称：" prop="name">
                    <Input v-model="formItem.name" size="large" placeholder="请填写内容..."/>
                </FormItem>
            </Col>
            <Col span="6">
                <FormItem label="品牌：" prop="brandId">
                    <Select v-model="formItem.brandId" size="large" style="width:100%" :label-in-value=true @on-change="changeBrand">
                        <Option v-for="item in brands" :value="item.id" :key="item.id">{{ item.name }}</Option>
                    </Select>
                </FormItem>
            </Col>
            <Col span="6">
                <FormItem label="供应商：" prop="supplierId">
                    <Select v-model="formItem.supplierId" size="large" style="width:100%" :label-in-value=true @on-change="changeSupplier">
                        <Option v-for="item in suppliers" :value="item.id" :key="item.id">{{ item.name }}</Option>
                    </Select>
                </FormItem>
            </Col>
            <Col span="6">
                <FormItem label="采购日期：" prop="purchaseDate">
                    <DatePicker v-model="formItem.purchaseDate" type="date" placeholder="请选择日期..." size="large" style="width:100%"></DatePicker>
                </FormItem>
            </Col>
        </Row>
        <Row type="flex">
            <Col span="6">
                <FormItem label="采购价格：" prop="price">
                    <Input v-model="formItem.price" size="large" placeholder="请输入价格..."/>
                </FormItem>
            </Col>
        </Row>
        <Row type="flex">
            <Col span="24">
                <FormItem>
                    <Button @click="save" size="large" type="primary">保存</Button>
                    <Button @click="close" size="large" type="ghost" style="margin-left: 8px">关闭</Button>
                </FormItem>
            </Col>
        </Row>
    </Form>
  </card>
</template>

<script>
export default {
    data () {
        return {
            baseURL: '/user/equipment',
            brandURL: '/user/brand',
            supplierURL: '/user/supplier',
            brands: [],
            suppliers: [],
            formItem: {
                id: '',
                name: '',
                brandId: '',
                brandName: '',
                supplierId: '',
                supplierName: '',
                purchaseDate: '',
                price: ''
            },
            ruleForm: {
                name: [
                    { required: true, message: '请填写名称', pattern: /.+/, trigger: 'change' }
                ],
                brandId: [
                    { required: true, message: '请选择品牌', pattern: /.+/, trigger: 'change' }
                ],
                supplierId: [
                    { required: true, message: '请选择供应商', pattern: /.+/, trigger: 'change' }
                ],
                purchaseDate: [
                    { required: true, message: '请选择采购日期', pattern: /.+/, trigger: 'change' }
                ],
                price: [
                    { required: true, message: '请输入采购价格', pattern: /.+/, trigger: 'change' }
                ]
            }
        }
    },
    methods: {
        init () {
            // 加载品牌
            this.loadBrand()
            // 记载供应商
            this.loadSupplier()
            // 表单初始化
            this.$bus.fillData(this.formItem, this.$route.query.row)
        },
        loadBrand () {
            let url = this.brandURL + '/findAll'
            this.$ajax.get(url).then((response) => {
                let {code, data, msg} = response.data
                if (code === 0) {
                    this.brands = data
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        loadSupplier () {
            let url = this.supplierURL + '/findAll'
            this.$ajax.get(url).then((response) => {
                let {code, data, msg} = response.data
                if (code === 0) {
                    this.suppliers = data
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        changeBrand (option) {
            this.formItem.brandId = option.value
            this.formItem.brandName = option.label
        },
        changeSupplier (option) {
            this.formItem.supplierId = option.value
            this.formItem.supplierName = option.label
        },
        save () {
            this.$refs['formItem'].validate((valid) => {
                if (valid) {
                    let url = this.baseURL + '/saveOrUpdate'
                    this.$ajax.post(url, this.formItem).then((response) => {
                        let {code, msg} = response.data
                        if (code === 0) {
                            this.$bus.alert('success', msg)
                            this.$bus.init(this.formItem)
                            this.resetForm()
                            if (this.formItem.id !== '') {
                                this.close()
                            }
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                }
            })
        },
        resetForm () {
            this.$refs.formItem.resetFields()
        },
        close () {
            this.$bus.closePageToLink()
        }
    },
    beforeRouteEnter (to, from, next) {
        next(vm => {
            vm.init()
        })
    }
}
</script>

<style>

</style>
